<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="建设项目id" prop="eprProjectId">
                <el-input
                  v-model="queryParams.eprProjectId"
                  placeholder="请输入建设项目id"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="起止时间" prop="eprDuration">
                <el-input
                  v-model="queryParams.eprDuration"
                  placeholder="请输入起止时间"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="发布日期" prop="eprDatePosted">
                <el-date-picker clearable size="small"
                  v-model="queryParams.eprDatePosted"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择发布日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="文件类型" prop="eprFileType">
                <el-select v-model="queryParams.eprFileType" placeholder="请选择文件类型" clearable size="small">
                  <el-option label="请选择字典生成" value="" />
                </el-select>
              </el-form-item>
              <el-form-item label="批文号" prop="eprApprovalNo">
                <el-input
                  v-model="queryParams.eprApprovalNo"
                  placeholder="请输入批文号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="编制单位名称" prop="eprCompileUnit">
                <el-input
                  v-model="queryParams.eprCompileUnit"
                  placeholder="请输入编制单位名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="编制单位组织代码" prop="eprCompileUnitCode">
                <el-input
                  v-model="queryParams.eprCompileUnitCode"
                  placeholder="请输入编制单位组织代码"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="资料：环评审批申请表" prop="eprData1">
                <el-input
                  v-model="queryParams.eprData1"
                  placeholder="请输入资料：环评审批申请表"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="资料：环评报告书" prop="eprData2">
                <el-input
                  v-model="queryParams.eprData2"
                  placeholder="请输入资料：环评报告书"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="资料：评审专家组意见" prop="eprData3">
                <el-input
                  v-model="queryParams.eprData3"
                  placeholder="请输入资料：评审专家组意见"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="资料：建设项目环境保护审批登记表" prop="eprData4">
                <el-input
                  v-model="queryParams.eprData4"
                  placeholder="请输入资料：建设项目环境保护审批登记表"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="资料：环评技术评估报告" prop="eprData5">
                <el-input
                  v-model="queryParams.eprData5"
                  placeholder="请输入资料：环评技术评估报告"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="资料：环保行政部门对建设项目的环评批复意见" prop="eprData6">
                <el-input
                  v-model="queryParams.eprData6"
                  placeholder="请输入资料：环保行政部门对建设项目的环评批复意见"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['environment:report:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['environment:report:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['environment:report:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['environment:report:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <!--      <el-table-column align="center" label="环评和批复ID" prop="eprId"/>-->
      <!--      <el-table-column align="center" label="建设项目id" prop="eprProjectId"/>-->
      <!--      <el-table-column align="center" label="起止时间" prop="eprDuration"/>-->
      <el-table-column align="center" label="发布日期" prop="eprDatePosted" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eprDatePosted, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" label="文件类型" prop="eprFileType"/>-->
      <el-table-column align="center" label="批文号" prop="eprApprovalNo"/>
      <el-table-column align="center" label="编制单位名称" prop="eprCompileUnit"/>
      <el-table-column align="center" label="编制单位组织代码" prop="eprCompileUnitCode"/>
      <el-table-column align="center" label="环评审批申请表" prop="eprData1">
        <template slot-scope="scope" v-if="scope.row.eprData1">
          <a :href="`${baseUrl}${scope.row.eprData1}`" target="_blank"
             class="buttonText">{{ scope.row.eprData1.toString().substring(scope.row.eprData1.lastIndexOf("/") + 1) }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="环评报告书(表)" prop="eprData2">
        <template slot-scope="scope" v-if="scope.row.eprData2">
          <a :href="`${baseUrl}${scope.row.eprData1}`" target="_blank"
             class="buttonText">{{ scope.row.eprData2.toString().substring(scope.row.eprData2.lastIndexOf("/") + 1) }}</a>
        </template>
      </el-table-column>
            <el-table-column align="center" label="评审专家组意见" prop="eprData3">
              <template slot-scope="scope"  v-if="scope.row.eprData3">
                <a :href="`${baseUrl}${scope.row.eprData3}`" target="_blank" class="buttonText">{{scope.row.eprData3.toString().substring(scope.row.eprData3.lastIndexOf("/")+1)}}</a>
              </template>
            </el-table-column>
            <el-table-column align="center" label="环境保护审批登记表" prop="eprData4">
              <template slot-scope="scope"  v-if="scope.row.eprData4">
                <a :href="`${baseUrl}${scope.row.eprData4}`" target="_blank" class="buttonText">{{scope.row.eprData4.toString().substring(scope.row.eprData4.lastIndexOf("/")+1)}}</a>
              </template>
            </el-table-column>
            <el-table-column align="center" label="环评技术评估报告" prop="eprData5">
              <template slot-scope="scope"  v-if="scope.row.eprData5">
                <a :href="`${baseUrl}${scope.row.eprData5}`" target="_blank" class="buttonText">{{scope.row.eprData5.toString().substring(scope.row.eprData5.lastIndexOf("/")+1)}}</a>
              </template>
            </el-table-column>
            <el-table-column align="center" label="环评批复意见" prop="eprData6">
              <template slot-scope="scope"  v-if="scope.row.eprData6">
                <a :href="`${baseUrl}${scope.row.eprData6}`" target="_blank" class="buttonText">{{scope.row.eprData6.toString().substring(scope.row.eprData6.lastIndexOf("/")+1)}}</a>
              </template>
            </el-table-column>
      <!--      <el-table-column align="center" label="备注" prop="remark"/>-->
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['environment:report:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['environment:report:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改环评和批复对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix  m-f-a-i" label-width="140px">
        <div class="content-left" style="width: 45%">
          <!--          <el-form-item label="建设项目id" prop="eprProjectId">-->
          <!--            <el-input v-model="form.eprProjectId" placeholder="请输入建设项目id"/>-->
          <!--          </el-form-item>-->
          <el-form-item label="起止时间" prop="eprDuration">
            <el-input v-model="form.eprDuration" placeholder="请输入起止时间"/>
          </el-form-item>
          <el-form-item label="发布日期" prop="eprDatePosted">
            <el-date-picker v-model="form.eprDatePosted" clearable
                            placeholder="选择发布日期"
                            size="small"
                            style="width: 100%;"
                            type="date"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <!--          <el-form-item label="文件类型" prop="eprFileType">-->
          <!--            <el-select v-model="form.eprFileType" placeholder="请选择文件类型" style="width: 100%;">-->
          <!--              <el-option label="请选择字典生成" value=""/>-->
          <!--            </el-select>-->
          <!--          </el-form-item>-->
          <el-form-item label="环评审批申请表" prop="eprData1">
            <FileUpload v-model="form.eprData1"/>
          </el-form-item>
          <el-form-item label="环评报告书" prop="eprData2">
            <FileUpload v-model="form.eprData2"/>
          </el-form-item>
          <el-form-item label="评审专家组意见" prop="eprData3">
            <FileUpload v-model="form.eprData3"/>
          </el-form-item>
        </div>

        <div class="content-left" style="float: right; width: 45%">
          <el-form-item label="批文号" prop="eprApprovalNo">
            <el-input v-model="form.eprApprovalNo" placeholder="请输入批文号"/>
          </el-form-item>
          <el-form-item label="编制单位名称" prop="eprCompileUnit">
            <el-input v-model="form.eprCompileUnit" placeholder="请输入编制单位名称"/>
          </el-form-item>
          <div class="line">
            <el-form-item label="编制单位组织代码" prop="eprCompileUnitCode">
              <el-input v-model="form.eprCompileUnitCode" placeholder="请输入编制单位组织代码"/>
            </el-form-item>
          </div>
          <div class="line">
            <el-form-item label="建设项目环境保护审批登记表" prop="eprData4">
              <FileUpload v-model="form.eprData4"/>
            </el-form-item>
          </div>
          <el-form-item label="环评技术评估报告" prop="eprData5">
            <FileUpload v-model="form.eprData5"/>
          </el-form-item>
          <div class="line">
            <el-form-item label="环保行政部门对建设项目的环评批复意见" prop="eprData6">
              <FileUpload v-model="form.eprData6"/>
            </el-form-item>
          </div>
        </div>

      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center; margin-top:30px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addReport, delReport, exportReport, getReport, listReport, updateReport} from "@/api/environment/report";
import {getToken} from "@/utils/auth";
import {InfoItem} from "@/api/enterprise/info";
import {isIntegerNotMust,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Report",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 环评和批复表格数据
      reportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eprProjectId: null,
        eprDuration: null,
        eprDatePosted: null,
        eprFileType: null,
        eprApprovalNo: null,
        eprCompileUnit: null,
        eprCompileUnitCode: null,
        eprData1: null,
        eprData2: null,
        eprData3: null,
        eprData4: null,
        eprData5: null,
        eprData6: null,
        epId:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();

  },
  methods: {

    /** 查询环评和批复列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listReport(this.queryParams).then(response => {
            this.reportList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        eprId: null,
        eprProjectId: null,
        eprDuration: null,
        eprDatePosted: null,
        eprFileType: null,
        eprApprovalNo: null,
        eprCompileUnit: null,
        eprCompileUnitCode: null,
        eprData1: null,
        eprData2: null,
        eprData3: null,
        eprData4: null,
        eprData5: null,
        eprData6: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.eprId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加环评和批复";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const eprId = row.eprId || this.ids
      getReport(eprId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改环评和批复";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.eprId != null) {
            updateReport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addReport(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const eprIds = row.eprId || this.ids;
      this.$confirm('是否确认删除环评和批复编号为"' + eprIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delReport(eprIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有环评和批复数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportReport(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
